package com.hzjh.h5mall.model.ads

import androidx.lifecycle.MutableLiveData
import com.hzjh.base.ext.data.FlowConst
import com.hzjh.base.ext.frame.JHBaseVM
import com.hzjh.base.ext.widget.dialog.JHAdSuccessDialog
import com.hzjh.base.ext.widget.dialog.JHAdTaskCompletedDialog
import com.hzjh.base.http.toParams
import com.hzjh.base.http.toVersionParams
import com.hzjh.base.utils.eventbus.FlowBus
import com.hzjh.base.utils.function.toJson
import com.hzjh.base.utils.other.ActivityStackManage
import com.hzjh.h5mall.BuildConfig
import com.hzjh.h5mall.bean.AdStatus
import com.hzjh.h5mall.bean.UpFileResBean
import com.hzjh.h5mall.bean.UpFileTypes
import com.hzjh.h5mall.bean.ads.AdsBannerBean
import com.hzjh.h5mall.bean.ads.AdsDetailBean
import com.hzjh.h5mall.bean.ads.AdsTaskDetailBean
import com.hzjh.h5mall.http.AdsApi
import com.hzjh.h5mall.http.MallApi
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File

class AdsModel : JHBaseVM() {
    val ifMock:Boolean = true && BuildConfig.DEBUG
    //广告状态 0-未接单 1-已接 2-取消 3-完成
    var status: MutableLiveData<AdStatus> = MutableLiveData(AdStatus.UNACCEPT)
    var banner:MutableLiveData<ArrayList<AdsBannerBean>> = MutableLiveData(ArrayList())
    var platformList:MutableLiveData<ArrayList<AdsTaskDetailBean>> = MutableLiveData(ArrayList())
    var upLoadImgUrl:MutableLiveData<UpFileResBean> = MutableLiveData()
    var adsDetailBean:MutableLiveData<AdsDetailBean> = MutableLiveData()
    var acceptTaskId:MutableLiveData<String> = MutableLiveData()
    var adsSubmitResult:MutableLiveData<Boolean> = MutableLiveData()

    //获取广告详情
    fun reqAdsDetail(id:String) {
        val params = HashMap<String, String>()
        params["id"] = "$id"
        makeCall<AdsDetailBean> {
            onRequest {
                AdsApi.create().reqAdsDetail(params)
            }

            onSuccess {
                it?.state=-1 //广告状态未接单状态
                adsDetailBean.value=it
            }

        }
    }
    //接受广告
    fun reqAdsAccept(id:String) {
        val params = HashMap<Any, Any>()
        params["id"] = "$id"
        makeCall {
            onRequest {
                AdsApi.create().reqAdsAccept(params.toVersionParams())
            }

            onSuccess {
                //广告接受成功
                setStatus(AdStatus.ACCEPT)
                JHAdSuccessDialog.Builder(ActivityStackManage.currentActivity()).setData(adsDetailBean.value!!.platforms.map {platform -> platform.remarks })
                    .setCanCancel(false)
                    .setCanceledOnTouchOutside(false)
                    .create().show()
                acceptTaskId.value=it
                //发送订单状态变更通知
                FlowBus.boolFlowBusNotify(FlowConst.REFRESH_ADS_STATUS,true)
            }

        }
    }
    //获取广告订单详情
    fun reqAdsOrderDetail(id:String) {
        val params = HashMap<String, String>()
        params["id"] = "$id"
        makeCall<AdsDetailBean> {
            onRequest {
                AdsApi.create().reqAdsOrderDetail(params)
            }

            onSuccess {
                adsDetailBean.value=it
            }
        }
    }
    //取消接单
    fun reqAdsCancel(id:String) {
        val params = HashMap<Any, Any>()
        params["id"] = "$id"
        makeCall {
            onRequest {
                AdsApi.create().reqAdsCancel(params.toVersionParams())
            }

            onSuccess {
                //广告取消成功
                setStatus(AdStatus.CANCEL)
                //发送订单状态变更通知
                FlowBus.boolFlowBusNotify(FlowConst.REFRESH_ADS_STATUS,true)
            }

        }
    }
    //提交审核
    fun reqAdsSubmit(adsTaskDetailBean: AdsTaskDetailBean) {
        makeCall {
            onRequest {
                AdsApi.create().reqAdsSubmit(adsTaskDetailBean.toJson().toParams())
            }

            onSuccess {
                //提交
                JHAdTaskCompletedDialog.Builder(ActivityStackManage.currentActivity())
                    .setCanCancel(false)
                    .setCanceledOnTouchOutside(false)
                    .setPlatform(adsTaskDetailBean.type)
                    .setContent("$it USD")
                    .setClickCloseLinstener { adsSubmitResult.value=true }
                    .create()
                    .show()
            }

        }
    }


    fun setBannerData(data:List<AdsBannerBean>){
        //获取banner数据
        banner.value?.clear()
        banner.value?.addAll(data)
        banner.postValue(banner.value)
    }

    //修改广告状态
    fun setStatus(state: AdStatus){
        status.value=state
    }

    //获取广告平台数据
    fun setPlatformData(data:List<AdsTaskDetailBean>){
        platformList.value?.clear()
        platformList.value?.addAll(data)
        platformList.postValue(platformList.value)
    }

    //图片上传
    fun uploadImage(url: String,type:UpFileTypes){
        makeCall<UpFileResBean> {
            onRequest {
                val file = File(url)
                val requestFile =
                    file.asRequestBody("multipart/form-data".toMediaTypeOrNull())
                val fileBody =
                    MultipartBody.Part.createFormData("multipart", file.name, requestFile)
                MallApi.create().uploadImage(fileBody)
            }

            onSuccess {
                it?.type=type
                upLoadImgUrl.value=it
            }

            onError { errMsg.value = it }
        }
    }

}